package com.umeox.moto.watch.themes.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageButton;

import com.umeox.moto.watch.themes.R;
import com.umeox.moto.watch.themes.app.ThemeManager;
import com.umeox.moto.watch.themes.drawable.RippleDrawable;
import com.umeox.moto.watch.themes.skin.loader.ResourceManager;


public class ImageButton extends AppCompatImageButton implements ThemeManager.OnThemeChangedListener {

    protected int mCurrentStyle = ThemeManager.THEME_UNDEFINED;
    private RippleManager mRippleManager;

    public ImageButton(Context context) {
        this(context, null, R.attr.u_imageButtonStyle);
    }

    public ImageButton(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.u_imageButtonStyle);
    }

    public ImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr, R.style.Widget_Uwear_ImageButton);
    }

    protected void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        applyStyle(context, attrs, defStyleAttr, defStyleRes);
        if (!isInEditMode()) {
            mCurrentStyle = ThemeManager.getInstance().getCurrentStyle(context);
        }
    }

    public void applyStyle(int resId) {
        applyStyle(getContext(), null, R.attr.u_imageButtonStyle, resId);
    }

    protected void applyStyle(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        getRippleManager().onCreate(this, context, attrs, defStyleAttr == 0 ? R.attr.u_imageButtonStyle : defStyleAttr, defStyleRes);
    }

    @Override
    public void onThemeChanged(ThemeManager.OnThemeChangedEvent event) {
        int style = ThemeManager.getInstance().getCurrentStyle(getContext());
        if (mCurrentStyle != style) {
            mCurrentStyle = style;
            applyStyle(mCurrentStyle);
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (mCurrentStyle != ThemeManager.THEME_UNDEFINED) {
            ThemeManager.getInstance().registerOnThemeChangedListener(this);
            onThemeChanged(null);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        RippleManager.cancelRipple(this);
        if (mCurrentStyle != ThemeManager.THEME_UNDEFINED) {
            ThemeManager.getInstance().unregisterOnThemeChangedListener(this);
        }
    }

    @Override
    public void setBackgroundDrawable(Drawable drawable) {
        Drawable background = getBackground();
        if (background instanceof RippleDrawable && !(drawable instanceof RippleDrawable)) {
            ((RippleDrawable) background).setBackgroundDrawable(drawable);
        } else {
            super.setBackgroundDrawable(drawable);
        }
    }

    @Override
    public void setImageResource(int resId) {
        super.setImageDrawable(ResourceManager.getInstance().getDrawable(resId));
    }

    protected RippleManager getRippleManager() {
        if (mRippleManager == null) {
            synchronized (RippleManager.class) {
                if (mRippleManager == null) {
                    mRippleManager = new RippleManager();
                }
            }
        }
        return mRippleManager;
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        RippleManager rippleManager = getRippleManager();
        if (l == rippleManager) {
            super.setOnClickListener(l);
        } else {
            rippleManager.setOnClickListener(l);
            setOnClickListener(rippleManager);
        }
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        boolean result = super.onTouchEvent(event);
        return getRippleManager().onTouchEvent(this, event) || result;
    }
}
